package com.lbd.project.service.impl;

import com.lbd.project.bean.TargetInfo;
import com.lbd.project.dao.TargetRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.project.bean.PersonalPaybackPlan;
import com.lbd.project.dao.PersonalPaybackPlanRepository;
import com.lbd.project.service.PersonalPaybackPlanService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 个人还款计划
 */
@Service
@Transactional
public class PersonalPaybackPlanServiceImpl extends BaseServiceImpl<PersonalPaybackPlan, PersonalPaybackPlanRepository>
        implements PersonalPaybackPlanService {

    @Autowired
    PersonalPaybackPlanRepository personalPaybackPlanRepository;
    @Autowired
    TargetRepository targetRepository;

    @Override
    public String buildJpql(PersonalPaybackPlan personalPaybackPlan, Map<String, Object> parameter) {
        return "";
    }

    @Override
    public Page listByTargetId(int page, int rows, String type, PersonalPaybackPlan personalPaybackPlan) {
        StringBuilder jpql = null;
        if ("1".equals(type)) {
            //还款中
            jpql = new StringBuilder("select min(p.qs) as qs,u.`name`,u.telephone as telephone,o.created_date as investDate" +
                    ",p.yhkr,p.bj,p.lx,p.hkrq, p.state,p.paid_bj as paidBj,p.paid_lx as paidLx,p.id" +
                    " from personal_payback_plan p,`user` u,`order` o where p.user_id=u.id and p.order_id=o.id" +
                    " and p.target_id=:target_id and p.state in('0','2','4')  " +
                    "group by p.order_id ");
        } else {
            //还款完成
            jpql = new StringBuilder("select p.qs as qs,u.`name`,u.telephone as telephone,o.created_date as investDate" +
                    ",p.yhkr,p.bj,p.lx,p.hkrq, p.state,p.paid_bj as paidBj,p.paid_lx as paidLx,p.id" +
                    " from personal_payback_plan p,`user` u,`order` o where p.user_id=u.id and p.order_id=o.id" +
                    " and p.target_id=:target_id  ");
        }
        Map<String, Object> parameter = new HashMap();
        parameter.put("target_id", personalPaybackPlan.getTargetId());
        Page<Map> personalPaybackPlans = findByPageNativeQuery(page, rows, parameter, jpql.toString());
        //查询对应标的
        TargetInfo info = targetRepository.findById(personalPaybackPlan.getTargetId());
        personalPaybackPlans.getContent().stream().forEach(plan -> {
            //总金额
            plan.put("totalAmount", ((BigDecimal) (plan.get("bj"))).add((BigDecimal) (plan.get("lx"))));
            plan.put("paidTotalAmount", (new BigDecimal((String) plan.get("paidBj"))).add(new BigDecimal((String) plan.get("paidLx"))));
            // 当期应还期数
            plan.put("qsAll", plan.get("qs") + "/" + info.getHkqs());
            //逾期天数
            if (plan.get("hkrq") != null) {
                //实际还款日期-应还款日期
                plan.put("yqts", getOverdueDate((Date) plan.get("hkrq"), (Date) plan.get("yhkr")));
            } else {
                //当前日期-应还款日期
                plan.put("yqts", getOverdueDate(new Date(), (Date) plan.get("yhkr")));
            }
        });
        return personalPaybackPlans;
    }

    /**
     * 计算逾期天数  当天不算逾期
     *
     * @param actualDate
     * @param planDate
     * @return
     */
    private String getOverdueDate(Date actualDate, Date planDate) {
        if (actualDate.before(planDate)) {
            return "0";//未逾期
        } else {
            long day = (actualDate.getTime() - planDate.getTime()) / (1000 * 60 * 60 * 24);
            return day + "";
        }
    }

}
